//
//  YXCamera.h
//  MiaoPaiVideoKitSample
//
//  Created by Makarov on 2017/6/1.
//  Copyright © 2017年 Makarov. All rights reserved.
//

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIView.h>
#import "YXVideoSegment.h"
#import "YXGLContext.h"
@class YXVideoSegment;
@class YXGLContext;

#define YXCameraUSEPreview

#define YXCameraAPI extern

// 拍摄采样方案
YXCameraAPI NSString *kYXCameraCapturePresetKey;
#if 0
{
#endif
    YXCameraAPI NSString *YXCameraCapturePreset640x480;
    YXCameraAPI NSString *YXCameraCapturePreset1280x720;
    YXCameraAPI NSString *YXCameraCapturePreset1920x1080;
#if 0
}
#endif

//拍摄视频的平均码流
YXCameraAPI NSString *kYXCameraCaptureVideoAverageBitRateKey;
//拍摄视频的宽 默认使用采样宽
YXCameraAPI NSString *kYXCameraCaptureVideoSizeWidthKey;
//拍摄视频的高 默认使用采样高
YXCameraAPI NSString *kYXCameraCaptureVideoSizeHeightKey;

@class YXVideoWriter;
@class YXCamera;
@class YXVideoSegment;

@protocol YXCameraDelegate <NSObject>
    
    @optional

    /**
     视频采样数据回调

     @param camera camera实例
     @param videoSampleBuffer 视频采样数据
     @param pixelBuffer 待赋值的处理后的视频数据 buffer指针 可选,如果不选,则修改后的数据不会被写入文件
     @param texture 待赋值的处理后的纹理指针 可选,如果不选,则再次上传 buffer 有显示要求则用于显示
     */
    - (void)camera:(YXCamera *)camera didOutputVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer insteadPixelBuffer:(CVPixelBufferRef *)pixelBuffer insteadTexture:(GLuint *)texture;


    /**
     音频采样数据回调

     @param camera camera实例
     @param audioSampleBuffer 音频采样数据
     */
    - (void)camera:(YXCamera *)camera didOutputAudioSampleBuffer:(CMSampleBufferRef)audioSampleBuffer;


    /**
     拍摄视频片段完成回调

     @param camera camera实例
     @param videoSegment 拍摄完成的视频片段
     @param error 错误返回,如果拍摄成功 error为空, 否则 videoSegment为空
     */
    - (void)camera:(YXCamera *)camera didRecordedVideoSegment:(YXVideoSegment *)videoSegment error:(NSError *)error;


    /**
     视频拍摄总长度回调

     @param camera camera实例
     @param duration 拍摄总长度
     */
    - (void)camera:(YXCamera *)camera didRecordedVideoSegmentDuration:(CMTime)duration;


    /**
     capture session is ready or not
     
     @param camera camera实例
     @param isPrepared ready ?
     */
    - (void)camera:(YXCamera *)camera sessionPrepared:(BOOL)isPrepared;

@end

@interface YXCamera : NSObject
    
    @property (nonatomic, weak) id<YXCameraDelegate> delegate;


    /**
     camera位置 (前置/后置) 默认后置
     */
    @property (nonatomic, assign) AVCaptureDevicePosition cameraPosition;


    /**
     拍摄中状态
     */
    @property (nonatomic, readonly) BOOL isRecording;



    /**
     拍摄视频的保存路径 缺省 tmp 下
     */
    @property (nonatomic, strong) NSString *videoStorageDirectory;


    /**
     拍摄视频的标识 (前缀 ) 缺省 YXVideo
     */
    @property (nonatomic, strong) NSString *videoStorageIdentifier;

#ifdef YXCameraUSEPreview

    /**
     预览 View
     需要对
     - (void)camera:(YXCamera *)camera didOutputVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer insteadPixelBuffer:(CVPixelBufferRef *)pixelBuffer insteadTexture:(GLuint *)texture;
     pixelBuffer 或 texture 赋值
     否则显示原 videoSampleBuffer
     */
    @property (nonatomic, readonly) UIView *previewView;

    @property (nonatomic, assign) BOOL enablePreview;
#endif


    /**
     初始化方法 需要
     kYXCameraCapturePresetKey
     kYXCameraCaptureVideoAverageBitRateKey
     kYXCameraCaptureVideoSizeWidthKey
     kYXCameraCaptureVideoSizeHeightKey
     的值
     @param configuration 配置字典
     @return camera实例
     */
    - (id)initWithConfiguration:(NSDictionary *)configuration;

    - (void)updateCaptureVideoSize:(CGSize)size;
    - (void)updateCapturePreset:(NSString *)preset;
    - (void)updateCaptureAverageBitRate:(NSUInteger)bitrate;


    /**
     准备拍摄
     
     @return 是否成功
     */
    - (BOOL)prepare;

    /**
     开始采样
     */
    - (void)startSampling;

    /**
     停止采样
     */
    - (void)stopSampling;

    /**
     对焦

     @param point 对焦点
     */
    - (void)focusAt:(CGPoint)point;

    /**
     切换前置/后置
     */
    - (void)switchCameraPosition;

    /**
     灯光辅助

     @param flash 是否开启
     @return 是否成功
     */
    - (BOOL)enableFlash:(BOOL)flash;


    /**
     开始拍摄片段
     */
    - (void)begineSegment;


    /**
     结束拍摄片段
     */
    - (void)endSegment;


    /**
     移除最近一次拍摄片段
     */
    - (void)removeLastSegment;


    /**
     移除所有拍摄片段
     */
    - (void)removeAllSegments;


    /**
     已经拍摄的所有片段

     @return 片段数组
     */
    - (NSArray<YXVideoSegment *> *)recordedSegments;


    @property (nonatomic, assign) BOOL outputPixelFormatAs420f;



/**
不加效果的拍摄
 
 @param handler callback
 */
- (void)captureStillImage:(void (^)(UIImage *outputImage))handler;


/**
 准备拍摄处理过的照片
 冻结画面
 */
- (void)prepareCaptureProcessedImage;

/**
 结束拍摄处理过的照片
 恢复画面
 */
- (void)endCaptureProcessedImage;

/**
 拍摄处理过的照片拿到图片

 @param handler callback
 */
- (void)captureProcessedImage:(void (^)(UIImage *outputImage))handler;

- (void)runSyncInSessionQueue:(dispatch_block_t)block;
- (void)runAsyncInSessionQueue:(dispatch_block_t)block;

@property (nonatomic, readonly) YXGLContext *context;
@property (nonatomic, assign) uint oFps;

@end

